package edu.cibertec.sicsolutions.vfs.votacion.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import edu.cibertec.sicsolutions.constants.Constants;
import edu.cibertec.sicsolutions.util.StringUtil;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Cedula;
import edu.cibertec.sicsolutions.vfs.domain.votacion.DetalleCedula;
import edu.cibertec.sicsolutions.vfs.domain.votacion.ElectorVirtual;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Empadronado;
import edu.cibertec.sicsolutions.vfs.domain.votacion.Escrutinio;
import edu.cibertec.sicsolutions.vfs.service.CedulaService;
import edu.cibertec.sicsolutions.vfs.service.ElectorVirtualService;
import edu.cibertec.sicsolutions.vfs.service.EmpadronadoService;
import edu.cibertec.sicsolutions.vfs.service.EscrutinioService;

public class VotacionAction  extends  DispatchAction{
	
	private static final Logger log = Logger.getLogger(VotacionAction.class);
	
	 public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 
		HttpSession session = request.getSession();
		session.setAttribute("logout", "loginElector.do?method=logout");
	try {
		  
		Cedula cedula = new Cedula();
		cedula.setCodigoTipoEleccion(1);
		cedula.setFlagVotacion(1);
		Cedula[] cedulaActiva = CedulaService.findCedulaByClass(cedula);
		if((cedulaActiva!=null && cedulaActiva.length>0)&& cedulaActiva[0]!=null ){ 
			 session.setAttribute("listCedulaActiva",cedulaActiva[0].getDetalleCedulas() );
		} 
	} catch (Exception e) {
		session.setAttribute("msjLogin", "Aplicacion no disponible");
		throw e;
	}
		return mapping.findForward(Constants.SUCCESS_INIT);
	}
	
	 public ActionForward votar(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
		 
		 HttpSession session = request.getSession();
		 try {
			 ElectorVirtual electorVirtual =  (ElectorVirtual)session.getAttribute("electorVirtual");
			  
			 if(electorVirtual.getFlagVotar().equals(0)){
				 Empadronado empadronado = new Empadronado(electorVirtual.getCodigoEmpadronado());
				 empadronado = EmpadronadoService.getEmpadronadoByPk(empadronado);
				 
				  Integer rowIdVotacion = -1;
				  Escrutinio escrutinio = new Escrutinio(); 
				  if(!StringUtil.isNullOrEmpty(request.getParameter("rowIdVotacion"))){
				       rowIdVotacion = Integer.parseInt(request.getParameter("rowIdVotacion")); 
					  DetalleCedula[] detalleCedulas =(DetalleCedula[])session.getAttribute("listCedulaActiva");			
					  escrutinio.setCodigoCandidato(detalleCedulas[rowIdVotacion].getCodigoCandidato() );				   
				  }
				escrutinio.setCodigoMesa(empadronado.getCodigoMesa());
				  
				EscrutinioService.insertEscrutinio(escrutinio);
				Map map = new HashMap();
				map.put("flagVotar",1);
				map.put("login",electorVirtual.getLogin());
				ElectorVirtualService.updateElectorVirtualByMap(map);
				return mapping.findForward(Constants.SUCCESS_MSG);
			 }
		 } catch (Exception e) {
				session.setAttribute("msjLogin", "Aplicacion no disponible");
				throw e;
			}
		 return mapping.findForward("noVota");
	 }
	 

}
